#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

int bad = 4;

bool isBadVersion(int version);

bool isBadVersion(int version)
{
	return version >= bad;
}


//int Find(int left, int end)
//{
//	if (left >= end)
//		return isBadVersion(left) ? left : end;
//	if (isBadVersion(left))
//		return left;
//	else if (end - left == 1)
//		return end;
//	int mid = left + (end - left) / 2;
//	return isBadVersion(mid) ? Find(left, mid) : Find(mid, end);
//}
//int firstBadVersion(int n) 
//{
//	return Find(1, n);
//}


//int firstBadVersion(int n)
//{
//	int left = 1;
//	int right = n;
//	int mid = 0;
//	// 1 5
//	//  3 
//	// 
//	while (left < right)
//	{
//		mid = (left + right) / 2;
//		if (isBadVersion(mid))
//		{
//			right = mid;
//		}
//		else
//		{
//			left = mid;
//		}
//	}
//	return mid;
//}
 

int Find(int* nums, int left, int right,int target,int num)
{
	assert(nums);
	if (left >= right)
	{
		if (left == 0)
			return 1;
		if (right == num - 1)
			return num;
		return left;
	}
	
	if (nums[left] == target)
		return left;
	if (nums[right] == target)
		return right;
	int mid = left + (right - left) / 2;
	if (nums[mid] == target)
		return mid;
	if (nums[mid] > target)
	{
		if (mid == left + 1)
			return mid;
		return Find(nums, left, mid - 1, target,num);
	}
	else
	{
		if (right == mid + 1)
			return mid;
		return Find(nums, mid + 1, right, target,num);
	}

}
int searchInsert(int* nums, int numsSize, int target)
{
	assert(nums);
	int left = 0;
	int right = numsSize - 1;
	while (left < right)
	{
		int mid = left + (right - left) / 2;
		if (nums[mid] == target)
			return mid;
		if (nums[mid] > target)
			right = mid - 1;
		else
			left = mid + 1;
	}
	return left;
}
int main()
{
	
	printf("%d\n", firstBadVersion(5));
	return 0;
}

